package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.LuatDao;
import com.tt.fashionWebSite.dao.TapLuatDao;
import com.tt.fashionWebSite.domain.Luat;
import com.tt.fashionWebSite.domain.TapLuat;
import com.tt.fashionWebSite.dto.JsonResponseCode;
import com.tt.fashionWebSite.dto.LuatDto;
import com.tt.fashionWebSite.dto.TapLuatDto;
import com.tt.fashionWebSite.service.Ad_LawService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_lawService")
public class Ad_LawServiceImp extends GenericService implements Ad_LawService {
    @Autowired
    @Qualifier("luat")
    private LuatDao luatDao;

    @Autowired
    @Qualifier("tapLuat")
    private TapLuatDao tapLuatDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminLaw(LuatDto lawDto) {
        Luat luat = luatDao.findById(lawDto.getMaLuat());
        if(luat == null) {
            luat = new Luat();
            luat.setMaLuat(lawDto.getMaLuat());
            luat.setTenLuat(lawDto.getTenLuat());
            luat.setgTKL(lawDto.getgTKL());
            luatDao.insert(luat);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    public List<LuatDto> getLawHypothesis() {
        return mapper.mapCollection(luatDao.getLawHypothesis(), LuatDto.class);
//        ThongTinThanhVienDto thongTinThanhVienDto = mapper.map(thongTinThanhVien, ThongTinThanhVienDto.class, THONG_TIN_THANH_VIEN_MAP_ID);
    }

    @Override
    public List<LuatDto> getLawReason() {
        return mapper.mapCollection(luatDao.getLawReason(), LuatDto.class);
//        ThongTinThanhVienDto thongTinThanhVienDto = mapper.map(thongTinThanhVien, ThongTinThanhVienDto.class, THONG_TIN_THANH_VIEN_MAP_ID);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminLawDelete(List<LuatDto> lawDto) {
        for(int i = 0; i < lawDto.size(); i++) {
            luatDao.delete(lawDto.get(i).getMaLuat());
        }

        return JsonResponseCode.SUCCESS;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminLawAddNew(TapLuatDto lawDto) {
        TapLuat tapLuat = new TapLuat();
        tapLuat.setVeTrai(lawDto.getVeTrai());
        tapLuat.setVePhai(lawDto.getVePhai());
        tapLuatDao.insert(tapLuat);

        return JsonResponseCode.SUCCESS;
    }
}
